/**
 * Copyright (c) 2018 杭州宏汉软件技术有限公司 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package com.ikun.common.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import static com.sun.org.apache.xml.internal.serializer.Method.UNKNOWN;

/**
 * @ClassName HttpContextUtils
 * @Description: 请求上下文工具
 * @CreateDate: 2024/4/1 15:15
 */
public class HttpContextUtils {

    public static HttpServletRequest getHttpServletRequest() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            return null;
        }

        return ((ServletRequestAttributes) requestAttributes).getRequest();
    }

    public static Map<String, String> getParameterMap(HttpServletRequest request) {
        Enumeration<String> parameters = request.getParameterNames();

        Map<String, String> params = new HashMap<>();
        while (parameters.hasMoreElements()) {
            String parameter = parameters.nextElement();
            String value = request.getParameter(parameter);
            if (StringUtils.isNotBlank(value)) {
                params.put(parameter, value);
            }
        }

        return params;
    }

    public static String getDomain() {
        HttpServletRequest request = getHttpServletRequest();
        StringBuffer url = request.getRequestURL();
        return url.delete(url.length() - request.getRequestURI().length(), url.length()).toString();
    }

    public static String getOrigin() {
        HttpServletRequest request = getHttpServletRequest();
        return request.getHeader(HttpHeaders.ORIGIN);
    }

    public static String getLanguage() {
        //默认语言
        String defaultLanguage = "zh-CN";
        //request
        HttpServletRequest request = getHttpServletRequest();
        if (request == null) {
            return defaultLanguage;
        }

        //请求语言
        defaultLanguage = request.getHeader(HttpHeaders.ACCEPT_LANGUAGE);

        return defaultLanguage;
    }

    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
    }

    /**
     * 获取当前请去地址
     * @return
     */
    public static String getRequestUri() {
        // 获取当前请求的 HttpServletRequest 对象
        HttpServletRequest httpServletRequest = getHttpServletRequest();
        // 获取请求地址
        return httpServletRequest.getRequestURI().toString();
    }

    /**
     * 获取当前ip
     * @return
     */
    public static String getIp() {
        // 获取当前请求的 HttpServletRequest 对象
        HttpServletRequest httpServletRequest = getHttpServletRequest();
        // 获取请求地址
        return httpServletRequest.getRemoteAddr();
    }

    /**
     * 获取当前接口请求的方法
     * @return
     */
    public static String getRequestMethod() {
        HttpServletRequest httpServletRequest = getHttpServletRequest();
        // 获取请求地址
        return httpServletRequest.getMethod();
    }


    public static String getRequestParams() {
        HttpServletRequest httpServletRequest = getHttpServletRequest();

        return JSONObject.toJSONString(httpServletRequest.getParameterMap());
    }




    private static HttpServletRequest getCurrentRequest() {
        return (HttpServletRequest) RequestContextHolder.currentRequestAttributes();
    }
}
